<?php
$loader->requireOnce('includes/ReportException.class.php');

/**
 * Default implement of report filter
 *  
 * @author Pham Trung Nguyen
 *
 */
abstract class AbsReportFilter {

    const DATE_SINGLE     = 'date_single';
    const DATE_FROM_TO    = 'date_from_to';
    const DATE_FULL       = 'date_full';
    
    /**
     * 
     * @var Controller
     */
    protected $controller   = NULL;
    protected $logger       = NULL;    
    protected $dateType     = NULL;
    
    /**
     * Constructor
     * @param $type String
     */
    public function __construct($controller) {                
        if (!($controller instanceof Controller)) {
            $this->logger->error();
            throw new ReportException("The object $controller is not instance of Controller");
        }                        
        $this->controller = $controller;  
    }
    
    
    public function setDateType($type) {
        if ($type != self::DATE_SINGLE &&
            $type != self::DATE_FROM_TO &&
            $type != self::DATE_FULL) {
            $this->logger->error();
            throw new ReportException("The date type '$type' is invalid");    
        }
        $this->dateType = $type;
    }
    
    /**
     * 
     * @return HTML Filter
     * @throws
     * 		ReportFilterException if 
     * 			$this->controller is not instance of Controller
     * 			$this->dateType is invalid
     * 		
     */
    public function renderFilter() {
        $result = '';
        $currentControllerPath = $this->controller->view->path;
        $this->controller->view->path = 'report_filter';    
        
        if ($this->dateType != NULL) {
            $result .= $this->renderDateFilter($this->controller, $this->dateType);    
        }        
        $this->controller->view->path = $currentControllerPath;
        
        $result .= $this->renderExtraFilter();        
        return $result;              
    }
    
    /**
     * Concrete report filter should implement this function to
     * build their own filter. 
     * @return HTML
     */
    protected abstract function renderExtraFilter();
    
    //========================= DATE FILTER ===========================
    
    private function renderDateFilter($controller, $type) {
        $orders	=	L10nHelper::getYMDorder();
        $this->controller->assign('datePart_0', $orders[0]);
        $this->controller->assign('datePart_1', $orders[1]);
        $this->controller->assign('datePart_2', $orders[2]);
        
        switch ($type) {
            case self::DATE_SINGLE:
                return $this->controller->view->render('date_single.html');
            case self::DATE_FROM_TO:
                return $this->controller->view->render('date_from_to.html');
            case self::DATE_FULL:
                $this->controller->assign('monthList', Utils::get12PreviousMonths());
		        $this->controller->assign('quarterList', Utils::get4PreviousQuaters());
                return $this->controller->view->render('date_full.html');    
        }               
    }
}